import time
from kafka import KafkaProducer

kafka_server = '192.168.1.134:9092'
topic = 'welog.event'


def connect():
    return KafkaProducer(bootstrap_servers=[kafka_server])


def send(message: str):
    msg = bytes(message, 'utf-8')
    future = producer.send(topic, key=b'2', value=msg, partition=0)

    while not future.is_done:
        time.sleep(0.1)

    print('发送成功！')


if __name__ == '__main__':
    global producer
    producer = connect()

    send('{id: 1, aid: 1, aid2: 2, fid: 441, action="v"}')
    send('{id: 2, aid: 1, aid2: 2, fid: 442, action="fc"}')
    send('{id: 3, aid: 1, aid2: 2, fid: 443, action="fc"}')
    send('{id: 4, aid: 1, aid2: 2, fid: 444, action="v"}')
